home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / handson.exe / FP1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-01-31  |  2.7 KB  |  100 lines

  1. unit Fp1;
  2. { PC Plus sample Delphi program.
  3.   This program shows how to call procedures and functions
  4.   and illustrates the difference between value parameters
  5.   and VAR parameters }
  6.  
  7. interface
  8.  
  9. uses
  10.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  11.   Forms, Dialogs, StdCtrls;
  12.  
  13. type
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Button1: TButton;
  20.     Button2: TButton;
  21.     Button3: TButton;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.     procedure Button3Click(Sender: TObject);
  25.   private
  26.     { Private declarations }
  27.   public
  28.     { Public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. { PROC1 PROCEDURE WITH VALUE PARAMETER                        }
  39. { Here the s parameter is passed 'by value'. In other words,
  40.   it is a copy of the original string. Any alterations made
  41.   to this copy, won't alter the original. So, in this case,
  42.   when the copy is set to LowerCase, the original remains
  43.   unchanged }
  44. procedure proc1( s : string );
  45. begin
  46.   s := LowerCase( s );
  47. end;
  48.  
  49. { PROC2 PROCEDURE WITH VAR PARAMETER                          }
  50. { Here s is a 'variable parameter'. This means it points to   }
  51. { (in effect, it *is*) the original string. When you change   }
  52. { a VAR parameter, the original string is changed too. In     }
  53. { this case, both the parameter, s, and the string in the     }
  54. { calling procedure will be changed to lower case.            }
  55. procedure proc2( var s : string );
  56. begin
  57.   s := LowerCase( s );
  58. end;
  59.  
  60.  
  61. { FUNCT1 A FUNCTION RETURNING A STRING                        }
  62. { Functions return a value of the type declared after the     }
  63. { colon in the Function heading - here ' : string;'           }
  64. { The value to be returned can be assigned to the Function    }
  65. { name itself - here 'Func1 :='                               }
  66. { As with a procedure, the parameter, 's', is left unchanged  }
  67. { if it is a value paramater but it would be changed if it    }
  68. { were a VAR parameter.                                       }
  69. { This function leaves s unchanged but returns a LowerCase    }
  70. { copy of s.                                                  }
  71. function Func1( s : string ) : string;
  72. begin
  73.   Func1 := LowerCase( s );
  74. end;
  75.  
  76. procedure TForm1.Button1Click(Sender: TObject);
  77. var
  78.    s : string;
  79. begin
  80.   s := Edit1.Text;
  81.   proc1(s);
  82.   Edit2.Text := s;
  83. end;
  84.  
  85. procedure TForm1.Button2Click(Sender: TObject);
  86. var
  87.    s : string;
  88. begin
  89.   s := Edit1.Text;
  90.   proc2(s);
  91.   Edit2.Text := s;
  92. end;
  93.  
  94. procedure TForm1.Button3Click(Sender: TObject);
  95. begin
  96.    Edit2.Text := Func1( Edit1.Text );
  97. end;
  98.  
  99. end.
  100.